<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Http\Services\RecruitService;
use App\Models\Company;
use App\Models\CompanyWelfare;
use App\Models\JobAddress;
use App\Models\JobColumn;
use App\Models\JobDelivery;
use App\Models\Recruit;
use Illuminate\Http\Request;

class RecruitController extends Controller
{   
    protected $RecruitService;
    public function __construct(RecruitService $RecruitService)
    {
        $this->RecruitService = $RecruitService;
    }

    public function index(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $is = Company::where('uid',$Request->uid)->first();
        if(!$is){
            return response()->json(['code' => '404', 'msg' => '请补全企业信息']);
        }
        $sql = Recruit::where('resource_id',$is->id)->with('org')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]['full_name'] = $is->full_name;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function lists(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $type = $Request->type ?: 2;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Recruit::select('id','title','city','areas','education','experience','full_name','province','is_recommend','address','salary','salary_type','eid','resource_id')->with('company')->with('company_xinzhu')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                });
        if($type == 2){
            $sql->where('is_recommend',1);
        }
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function resume(Request $Request)
    {
        $data = JobDelivery::where('jid',$Request->id)->with('resumes')->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function info(Request $Request)
    {
        $data = Recruit::find($Request->id);
        $data->RecruitTypeArr = Recruit::RecruitType;
        $data->EDU = Recruit::EDU;
        $data->EXP = Recruit::EXP;
        $data->WAGES = Recruit::YEAR_WAGES;
        if($data->salary_type == 2){
            $data->WAGES = Recruit::MONTH_WAGES;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function edit(Request $Request)
    {
        //查询企业信息
        $is = Company::where('uid',$Request->uid)->first();
        if(!$is){
            return response()->json(['code' => '404', 'msg' => '请补全企业信息']);
        }
        $input = $Request->input();
        $input['resource_id'] = $is->id;
        $input['full_name'] = $is->full_name;
        $input['uid'] = $Request->uid;
        $data = $this->RecruitService->update($input);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '请选择要删除的记录']);
        }
        $data = Recruit::find($Request->id);
        if(!$data){
            return response()->json(['code' => '403', 'msg' => '记录不存在']);
        }
        $data->delete();
        return response()->json(['code' => '200', 'msg' => 'success']);
    }

    public function companyEdit(Request $Request)
    {
        // if(!$Request->log){
        //     return response()->json(['code' => '403', 'msg' => '请上传logo']);
        // }
        // if(!$Request->scale){
        //     return response()->json(['code' => '403', 'msg' => '请选择规模']);
        // }
        if(!$Request->full_name){
            return response()->json(['code' => '403', 'msg' => '请输入企业名称']);
        }
        // if(!$Request->introduce){
        //     return response()->json(['code' => '403', 'msg' => '请输入介绍']);
        // }
        // if(!$Request->areas){
        //     return response()->json(['code' => '403', 'msg' => '请选择地区']);
        // }
        // if(!$Request->address){
        //     return response()->json(['code' => '403', 'msg' => '请输入地址']);
        // }
        $input = $Request->input();
        $input['uid'] = $Request->uid;
        $input['eid'] = $Request->eid;
        $data = $this->RecruitService->companyUpdate($input);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function companyInfo(Request $Request)
    {
        if($Request->eid){
            $data = Company::where('eid',$Request->eid)->first();
        }else{
            $data = Company::where('uid',$Request->uid)->first();
        }
        
        if(empty($data)){
            $data = [];
        }
        $welfare = CompanyWelfare::where('eid',0)->get();
        foreach ($welfare as $key => $value) {
            $welfare[$key]->name = $value->label;
            $welfare[$key]->value = (string)$value->id;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'welfare'=>$welfare]);
    }

    public function columns(Request $Request)
    {
        $search = $Request->search;
        $pid = $Request->pid;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = JobColumn::with('children')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                });

        if($pid == 'children'){
            $sql->where('pid','>',0);
        }else{
            $sql->where('pid',0);
        }
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function address(Request $Request)
    {
        $sql = JobAddress::where('eid',0);
        $data = $sql->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function welfare()
    {
        $data = CompanyWelfare::where('eid',0)->get();
        foreach ($data as $key => $value) {
            $data[$key]->name = $value->label;
            $data[$key]->value = (string)$value->id;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function columnUpdate(Request $Request)
    {
        $data = $this->RecruitService->updateType($Request->input());
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    
}
